//
//  URLFactory.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-17.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "URLFactory.h"
#import "UrlParameterFactory.h"
#import "InstallUrl.h"
#import "LFAccount.h"
#import "CoreDataManager.h"
#import "JSON.h"
#import "CONSTS.h"

@interface URLFactory() 



@end

@implementation URLFactory

#pragma mark -  获取登陆的第一个api的url-------- 登陆的第一步，获取登陆url 和 key(登陆的第二个api需要)
+ (NSString *)get_com_taobao_wireless_mtop_getLoginUrl
{
    /*样例
     http://api.m.taobao.com/rest/api2.do?api=com.taobao.wireless.mtop.getLoginUrl&v=*&i mei=357646.00.112178.2&imsi=
     8986011030452016267j&appkey=12241231&data={"a ppkey":"12241231"}&t=1314718746&sign=3423d32d11b2f15e2b29c58dd051a802&ttid =400000_//12241231@yssdk_android_1.0.0
     */
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    
    NSString *api = com_taobao_wireless_mtop_getLoginUrl;
    [urlParameterDic setObject: api forKey: @"api"];
    
    NSString *v = getV();
    [urlParameterDic setObject: v forKey: @"v"];
    
    NSString *imei = getImei();
    [urlParameterDic setObject: imei forKey: @"imei"];
    
    NSString *imsi = getImsi();
    [urlParameterDic setObject: imsi forKey: @"imsi"];
    
    [urlParameterDic setObject: appkey forKey: @"appkey"];
    
    NSString *data = getLoginData();
    [urlParameterDic setObject: data forKey: @"data"];
    
    NSString *t = getT();
    [urlParameterDic setObject: t forKey: @"t"];
    
    NSString *sign = getLoginSign();
    [urlParameterDic setObject: sign forKey: @"sign"];
    
    [urlParameterDic setObject: ttid forKey: @"ttid"];
    
    NSString *api_com_taobao_wireless_mtop_getLoginUrl = [InstallUrl getUrlWith: urlParameterDic 
                                                             addSystemParameter: NO 
                                                                            api: API_LOGIN];
    
    ITTDINFO(@"登陆组装完成\n = %@", api_com_taobao_wireless_mtop_getLoginUrl);
    
    return api_com_taobao_wireless_mtop_getLoginUrl;
}

#pragma mark - 获取登陆的第二个api的url-------- 获取token 和 topsession 和 sid/wapsesstion
+ (NSString *)get_com_taobao_client_mtop_getUserSessionKey
{
    /*样例
     http://api.m.taobao.com/rest/api2.do?api=com.taobao.client.mtop.getUserSessionKey&v =*&imei=357646.00.112178.2&imsi=8986011030452016267j&appkey=12241231&data ={"key":"c0870ca9-db7a-4b94-ac60-7daf1da63344","appkey":"12241231"}&t=13147 18830&sign=5851d1d5c54c28a3ef2e23d5cabb2919&ttid=400000_12241231@yssdk_a ndroid_1.0.0
     */
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    
    NSString *api = com_taobao_client_mtop_getUserSessionKey;
    [urlParameterDic setObject: api forKey: @"api"];
    
    NSString *v = getV();
    [urlParameterDic setObject: v forKey: @"v"];
    
    NSString *imei = getImei();
    [urlParameterDic setObject: imei forKey: @"imei"];
    
    NSString *imsi = getImsi();
    [urlParameterDic setObject: imsi forKey: @"imsi"];
    
    [urlParameterDic setObject: appkey forKey: @"appkey"];
    
    {   //组织和获取url中的data
        CoreDataManager *coredataManager = [CoreDataManager sharedCoreDataManager];
        LFAccount *account = [coredataManager getCurrentLoggedInAccount];
        NSString *key = account.key;
        
        NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithCapacity: 2];
        [dataDic setObject: key forKey: @"key"];
        [dataDic setObject: appkey forKey: @"appkey"];
        
        NSString *dataStr = [dataDic JSONRepresentation];
        ITTDINFO(@"dataStr = %@", dataStr);
        
        [urlParameterDic setObject: dataStr forKey: @"data"];
        
        NSString *t = getT();
        [urlParameterDic setObject: t forKey: @"t"];
        
        NSString *sign = getLoginSign();
        [urlParameterDic setObject: sign forKey: @"sign"];
        
        [urlParameterDic setObject: ttid forKey: @"ttid"];
        
        NSString *api_com_taobao_client_mtop_getUserSessionKey = [InstallUrl getUrlWith: urlParameterDic 
                                                                     addSystemParameter: NO 
                                                                                    api: API_LOGIN];
        
        return api_com_taobao_client_mtop_getUserSessionKey;
    }
}

/*                  交易api                   */
+ (NSString *)get_taobao_trades_sold_get:(NSString *)status_    //  获取卖家三个月内的交易
{
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    [urlParameterDic setObject: taobao_trades_sold_get forKey: @"method"];
    [urlParameterDic setObject: taobao_trades_sold_get_element forKey: @"fields"];
    ITTDINFO(@"%@", g_start_created);
    [urlParameterDic setObject: g_start_created forKey: @"start_created"];
    [urlParameterDic setObject: g_end_created forKey: @"end_created"];
    [urlParameterDic setObject: g_page_no forKey: @"page_no"];
    [urlParameterDic setObject: g_page_size forKey: @"page_size"];
    /*
     type:
     交易类型列表，同时查询多种交易类型可用逗号分隔。
     默认同时查询guarantee_trade, auto_delivery, ec, cod的4种交易类型的数据 。
     #define   @"一口价"
     #define  @"拍卖" 
     #define  @"一口价、拍卖"
     #define  @"旺店入门版交易"
     #define  @"旺店标准版交易" 
     #define  @"自动发货"
     #define  @"直冲" 
     #define  @"货到付款"
     #define  @"游戏装备"
     #define  @"ShopEX交易"
     #define  @"万网交易"
     #define  @"统一外部交易"
     #define  @"即时到账"
     #define  @"大商家货到付款"
     #define  @"酒店类型交易"
     #define  @"商超交易"
     #define  @"商超货到付款交易"
     */
    
    NSString *type = [NSString stringWithFormat: @"%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@", 
                      @"fixed",
                      @"auction",
                      @"guarantee_trade",
                      @"independent_simple_trade",
                      @"independent_shop_trade",
                      @"auto_delivery",
                      @"ec",
                      @"cod",
                      @"game_equipment",
                      @"shopex_trade",
                      @"netcn_trade",
                      @"external_trade",
                      @"instant_trade",
                      @"b2c_cod",
                      @"hotel_trade",
                      @"super_market_trade",
                      @"super_market_cod_trade"];
    
    [urlParameterDic setObject: type forKey: @"type"];
    if (status_ && !([status_ isEqualToString: @""]))
    {
        [urlParameterDic setObject: status_ forKey: @"status"];
    }
    
    NSString *api_taobao_trades_sold_get = [InstallUrl getUrlWith: urlParameterDic 
                                               addSystemParameter: YES 
                                                              api: API];
    
    ITTDINFO(@"%@", api_taobao_trades_sold_get);
    
    return api_taobao_trades_sold_get;
}

+ (NSString *)search_taobao_trades_sold_get:(NSDictionary *)pDic_ page_no:(NSString *)page_no_
{
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionaryWithDictionary: pDic_];
    [urlParameterDic setObject: taobao_trades_sold_get forKey: @"method"];
    [urlParameterDic setObject: page_no_ forKey: @"page_no"];
    [urlParameterDic setObject: SEARCH_REAULT_PAGE_SIZE forKey: @"page_size"];
    
    NSString *api_taobao_trades_sold_get = [InstallUrl getUrlWith: urlParameterDic 
                                               addSystemParameter: YES 
                                                              api: API];
    
    ITTDINFO(@"%@", api_taobao_trades_sold_get);
    
    return api_taobao_trades_sold_get;
}

+ (NSString *)get_com_taobao_client_sys_autologin
{
    /*
     http://api.m.taobao.com/rest/api2.do?
     api=com.taobao.client.sys.autologin&
     v=*&
     imei=35 7646.00.112178.2&
     imsi=8986011030452016267j&
     appkey=12241231&
     data={"token":" 7660735181ca52e98da27d63acf4484914091eb42e46e4d92e44ca82fc58b09ff70946f8c 58b5eb846d433bb8f219756a374cee495d64a5f28bf33d969fe5ef29d8d89c27e500c9c68 a8de5b9ce55848c67cd48e021245d43ef33c3a677a7c2feee9d028ac908f2874ef7181ed8 49c8b1409abd8706afc16fe09d72440b68b02","appkey":"12241231","toptoken":"6107dd aa023407edaa30eeb0a20f2140"}&
     t=1314718835&
     sign=fc7b9745235960d8d1c1dd358 37c1f9b&
     ttid=400000_12241231@yssdk_android_1.0.0
     */
    /*
     http://api.m.taobao.com/rest/api2.do?
     api=com.taobao.client.sys.autologin&
     appkey=12286611&
     data=%7B%22token%22%3A%2263bdc096c76eec237031d6aacd150c1df24d1b9a34b3a405c355d61399b265c879cd14808fe2e13565785d8e70b8b851db2572a6e867dc8d163d8e44b084960b5d6d6681c8a4bb36b2994035eb0bf3e57e340a3ec449e3ad86a76cd2956b40bc0902901330a39009233e9b2199d7a03388009957211f160b8e41054227af860f%22%2C%22appkey%22%3A%2212286611%22%2C%22toptoken%22%3A%2243b72c6624696336072765e2a291fcb7%22%7D&
     imei=CC32F56ADC37514&
     imsi=010FDE4C10DDB6&
     sign=6f5effa88b7967358e97c04564da9175&
     t=1322213604&
     ttid=400000_12286611%40wgwl_ios_1.0_test&
     v=%2A
     */
    NSString *api = api_com_taobao_client_sys_autologin;
    //NSString *token = getToken();
    //NSString *appKey = appkey;
    NSString *data = getDataForAutoLogin();
    ITTDINFO(@"%@", data);
    //IMEI或IMSI不匹配
    NSString *imei = getImei();
    NSString *imsi = getImsi();
    NSString *t = getT();
    
    
    
    
    
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionaryWithCapacity: 4];
    [urlParameterDic setObject: @"*" forKey: @"v"];
    [urlParameterDic setObject: api forKey: @"api"];
    //  [urlParameterDic setObject: token forKey: @"token"];
    [urlParameterDic setObject: appkey forKey: @"appkey"];
    [urlParameterDic setObject: data forKey: @"data"];
    [urlParameterDic setObject: imei forKey: @"imei"];
    [urlParameterDic setObject: imsi forKey: @"imsi"];
    [urlParameterDic setObject: t forKey: @"t"];
    [urlParameterDic setObject: ttid forKey: @"ttid"];
    
    
    
    
    NSString *sign = getSign(urlParameterDic);
    [urlParameterDic setObject: sign forKey: @"sign"];
    
    
    
    
    NSString *api_com_taobao_client_sys_autologin_str = [InstallUrl getUrlWith: urlParameterDic 
                                                            addSystemParameter: NO 
                                                                           api: API_LOGIN];
    ITTDINFO(@"自动登录 = %@", api_com_taobao_client_sys_autologin_str);
    
    return api_com_taobao_client_sys_autologin_str;
}

//获取订单详情
+ (NSString *)get_taobao_trade_fullinfo_getWithTid:(NSString *)tid_
{
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    [urlParameterDic setObject: api_taobao_trade_fullinfo_get forKey: @"method"];
    [urlParameterDic setObject: api_taobao_trade_fullinfo_get_element forKey: @"fields"];
    [urlParameterDic setObject: tid_ forKey: @"tid"];
    NSString *api_taobao_trade_fullinfo_get_element_str = [InstallUrl 
                                                           getUrlWith: urlParameterDic 
                                                           addSystemParameter: YES 
                                                           api: API];
    
    return api_taobao_trade_fullinfo_get_element_str;
}

//交易api
//添加备忘或更新备忘
//需要商家或以上权限才可调用此接口，可重复调用本接口更新交易备忘，本接口同时具有添加备忘的功能
+ (NSString *)get_taobao_trade_memo_updateWithTid:(NSString *)tid_ memo:(NSString *)memo_ flag:(NSString *)flag_ reset:(NSString *)reset_
{
    /*
     tid	Number	必须	123		 交易编号
     memo	String	可选	交易备忘		 交易备忘。最大长度: 1000个字节
     flag	Number	可选	1		 交易备忘旗帜，可选值为：0(灰色), 1(红色), 2(黄色), 3(绿色), 4(蓝色), 5(粉红色)，默认值为0
     reset	Boolean	可选	true	false	 是否对memo的值置空 若为true，则不管传入的memo字段的值是否为空，都将会对已有的memo值清空，慎用； 若用false，则会根据memo是否为空来修改memo的值：若memo为空则忽略对已有memo字段的修改，若memo非空，则使用新传入的memo覆盖已有的memo的值
     */
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    [urlParameterDic setObject: api_taobao_trade_memo_update forKey: @"method"];
    
    [urlParameterDic setObject: tid_ forKey: @"tid"];
    
    [urlParameterDic setObject: memo_ forKey: @"memo"];
    [urlParameterDic setObject: flag_ forKey: @"flag"];
    [urlParameterDic setObject: reset_ forKey: @"reset"];
    
    NSString *api_taobao_trade_memo_update_str = [InstallUrl getUrlWith: urlParameterDic 
                                                     addSystemParameter: YES 
                                                                    api: API];
    
    return api_taobao_trade_memo_update_str;
}

+ (NSString *)get_taobao_trade_receivetime_delay:(NSString *)yanChangShiJian_ tid:(NSString *)tid_
{
    /*
     taobao.trade.receivetime.delay 延长交易收货时间
     
     名称         类型          是否必须        示例值             默认值             描述
     tid        Number          必须          129381983                       主订单号
     days       Number          必须	3                                           延长收货的天数，可选值为：3, 5, 7, 10。
     */
    
    if (!yanChangShiJian_ || !([yanChangShiJian_ isEqualToString: @"3"] || [yanChangShiJian_ isEqualToString: @"5"] || [yanChangShiJian_ isEqualToString: @"7"] || [yanChangShiJian_ isEqualToString: @"10"]))
    {
        return nil;
    }
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    [urlParameterDic setObject: api_taobao_trade_receivetime_delay forKey: @"method"];
    
    [urlParameterDic setObject: yanChangShiJian_ forKey: @"days"];
    
    [urlParameterDic setObject: tid_ forKey: @"tid"];
    
    
    NSString *api_taobao_trade_memo_update_str = [InstallUrl getUrlWith: urlParameterDic 
                                                     addSystemParameter: YES 
                                                                    api: API];
    
    return api_taobao_trade_memo_update_str;
}

+ (NSString *)get_taobao_trade_close:(NSString *)reason_ tid:(NSString *)tid_
{
    /*
     tid	Number	必须	2231978090		 主订单或子订单编号。
     close_reason	String	必须	买家不想购买了		 交易关闭原因。
     */
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    [urlParameterDic setObject: api_taobao_trade_close forKey: @"method"];
    
    [urlParameterDic setObject: reason_ forKey: @"close_reason"];
    
    [urlParameterDic setObject: tid_ forKey: @"tid"];
    
    
    NSString *api_taobao_trade_memo_update_str = [InstallUrl getUrlWith: urlParameterDic 
                                                     addSystemParameter: YES 
                                                                    api: API];
    
    return api_taobao_trade_memo_update_str;
}

//获取单个交易的“物流详情”
+ (NSString *)get_taobao_logistics_orders_detail_get:(NSString *)tid_
{
    //taobao.logistics.orders.detail.get
    
    NSMutableDictionary *urlParameterDic = [NSMutableDictionary dictionary];
    
    [urlParameterDic setObject: api_taobao_logistics_orders_detail_get forKey: @"method"];
    
    [urlParameterDic setObject:   @"tid,order_code,seller_nick,buyer_nick,delivery_start,delivery_end,out_sid,item_title,receiver_name,receiver_phone,receiver_mobile,receiver_location,status,type,freight_payer,seller_confirm,company_name,is_success,created,modified" forKey: @"fields"];
   // [urlParameterDic setObject:   @"tid,receiver_location" forKey: @"fields"];
    [urlParameterDic setObject: tid_ forKey: @"tid"];
    
    NSString *api_taobao_logistics_orders_detail_get_str = [InstallUrl getUrlWith: urlParameterDic 
                                                     addSystemParameter: YES 
                                                                    api: API];
    
    return api_taobao_logistics_orders_detail_get_str;
}


@end
